"""
@File    : judgmentPortStatus.py
@Author  : qve-wzy
@Time    : 2020/9/3 0003 9:33
"""
#coding=utf-8
from util.dos_CMD import DosCmd
class JudgmentPortStatus:
    '''
    判断端口号的状态
    '''
    def port_used(self,port_num):
        flag = None
        self.dos = DosCmd()
        command = 'netstat -ano | findstr ' + str(port_num)
        result = self.dos.excute_cmd_result(command)
        if  len(result) > 0:
            flag = True
        else:
            flag = False
        return flag

    def create_port_list(self,star_port,devices):
        '''
        创建可用端口号
        '''
        port_list = []
        if  devices != None:
            while len(port_list) != len(devices):
                if self.port_used(star_port) != True:
                    # 执行dos命令没有对应的端口信息，则认为该端口号没有被占用，可以创建端口号
                    port_list.append(star_port)
                star_port = star_port +1
            return port_list
        else:
            print("生成可用端口号失败")
            return None
if __name__ == '__main__':
    port = JudgmentPortStatus()
    list = [1, 2, 3, 4, 5]
    print(port.create_port_list(4722, list))
    pass











